##############################################################
# Dynamic DiD with Covariates: All Visas and Crime Concerns
##############################################################

rm(list=ls())

library(Hmisc)
library(readstata13)
library(foreign)
library(tidyverse)
library(did)
library(ggplot2)
set.seed(1111)

#sink("07_results_rates_all.txt")

############################
# Read and prepare data 
############################

# load data
load("final_county_2023april6.RData")
names(d_county)

#######################
# Results 
#######################

# d-did 
out6 <- att_gt(yname = "crime_rates_s",
               gname = "first.treat_all",
               idname = "county",
               tname = "survey",
               xformla = ~turnout_2013 + income_2003 + population_2011 + right_voteshare_2013,
               data = d_county)

es6 <- aggte(out6, type = "dynamic",na.rm = TRUE)
summary(es6)
ggdid(es6)

# save results for overall effect
pe6 <- es6$overall.att
se6 <- es6$overall.se
results6 <- rbind(pe6,se6) 
results6
write.table(results6, "overallATT_6.txt", sep="\t")

# table a20
sink(file = "tableA20.txt")
summary(es6)
sink(file = NULL)

sink()
